#!/usr/bin/env python
import os.path
from ssummo.traverse import dict_walk


def combine_dicts(results_dicts, merge=False):
    """Give a list of SSUMMO results dictionaries. This shall return a
    dictionary containing each & every node from all of those dictionaries.

    Where accessions are found assigned to a node, this will combine the
    accessions from all results_dicts into a list of lists; one list of
    accessions per results dictionary, in the same order as are passed
    to this function."""
    def combine_accs( combined_node, results_node, ind ):
        if 'accessions' in combined_node.keys():
            combined_node['accessions'][ind] = results_node['accessions']
        else:
            combined_node.update( { 'accessions' : [ [] for i in xrange( len(results_dicts) ) ] } )
            combined_node['accessions'][ind] = results_node['accessions']
        return combined_node['accessions']

    combined_results = {}
    ind = 0
    if merge:
        combined_accessions = lambda combined_node, results_node, ind : \
            combined_node['accessions'] + results_node['accessions'] \
            if 'accessions' in combined_node else results_node['accessions']
    else:
        combined_accessions = combine_accs
    for results_dict in results_dicts:
        for full_path, results_node in dict_walk('', results_dict):
            if full_path == '':
                continue
            path_list = full_path.split(os.path.sep)
            combined_node = combined_results
            for node_name in path_list:
                if node_name in combined_node.keys():
                    combined_node = combined_node[node_name]
                else:
                    combined_node.update({ node_name : {} })
                    combined_node = combined_node[node_name]
            if 'accessions' in results_node.keys():
                combined_node.update({ 'accessions' :
                                     combined_accessions(combined_node,
                                                         results_node) })
                if 'accessions' in combined_node.keys():
                    combined_node['accessions'] += results_node['accessions']
                else:
                    combined_node.update({ 'accessions' : results_node['accessions'] })
        ind += 1
    return combined_results

def merge_dicts( results_dicts ):
    """Give a list of SSUMMO results dictionaries. This shall return a
    dictionary containing each & every node from all of those dictionaries.

    Where accessions are found assigned to a node, this will combine the
    accessions from all results_dicts into a list of lists; one list of
    accessions per results dictionary, in the same order as are passed
    to this function."""
    combined_results = {}
    ind = 0
    for results_dict in results_dicts:
        for full_path, results_node in dict_walk('', results_dict):
            if full_path == '':
                continue
            path_list = full_path.split(os.path.sep)
            combined_node = combined_results
            for node_name in path_list:
                if node_name in combined_node.keys():
                    combined_node = combined_node[node_name]
                else:
                    combined_node.update({ node_name : {} })
                    combined_node = combined_node[node_name]
            if 'accessions' in results_node.keys():
                #combined_node.update( { 'accessions' : combined_accessions( combined_node, results_node ) } )
                if 'accessions' in combined_node.keys():
                    combined_node['accessions'] += results_node['accessions']
                else:
                    combined_node.update({ 'accessions' : results_node['accessions'] })
        ind += 1
    return combined_results
